package com.sjy.ylxt.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.domain.YlxtAdmin;
import com.sjy.ylxt.entity.dto.AddAdminDTO;
import com.sjy.ylxt.entity.dto.QueryAdminDTO;
import com.sjy.ylxt.enums.DeleteFlagEnum;
import com.sjy.ylxt.factory.O2O;
import com.sjy.ylxt.service.YlxtAdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * @Author: 史俊颖
 * @CreateTime: 2024-12-12
 * @Description: 管理员控制类
 */

/**
 * 管理员管理
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private YlxtAdminService adminService;

    /**
     * 分页查询管理员
     */
    @PostMapping("/page-list")
    public ResultBody<?> queryAdmin(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return adminService.queryAdmin(pageNum,pageSize);
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    @PostMapping("/detail")
    public ResultBody<?> detail(@RequestParam(value = "id")String id) {
        return adminService.detail(id);
    }

    /**
     * 添加管理员
     * @return
     */
    @PostMapping("/add")
    public ResultBody<?> addAdmin(
            @RequestParam @NotBlank(message = "管理员姓名不能为空") String adminName,
            @RequestParam @NotBlank(message = "手机号不能为空") String phoneNumber,
            @RequestParam @NotBlank(message = "密码不能为空") String passWord) {
        AddAdminDTO addAdminDTO = new AddAdminDTO();
        addAdminDTO.setAdminName(adminName);
        addAdminDTO.setPhoneNumber(phoneNumber);
        addAdminDTO.setPassWord(passWord);
        return adminService.addAdmin(addAdminDTO);
    }


    /**
     * 禁用管理员
     */
    @PostMapping("/disable")
    public ResultBody<?> disableAdmin(@RequestParam(value = "id")String id) {
        return adminService.disable(id);
    }

    /**
     * 编辑管理员
     */
    @PostMapping("/edit")
    public ResultBody<?> editAdmin(
            @RequestParam @NotBlank(message = "管理员姓名不能为空") String adminName,
            @RequestParam @NotBlank(message = "手机号不能为空") String phoneNumber,
            @RequestParam @NotBlank(message = "密码不能为空") String passWord,
            @RequestParam @NotBlank(message = "id不能为空") String id) {
        AddAdminDTO addAdminDTO = new AddAdminDTO();
        addAdminDTO.setAdminName(adminName);
        addAdminDTO.setPhoneNumber(phoneNumber);
        addAdminDTO.setPassWord(passWord);
        addAdminDTO.setPassWord(addAdminDTO.getPassWord());
        YlxtAdmin ylxtAdmin = O2O.AdminDtoToEntity(addAdminDTO);
        ylxtAdmin.setDeleteFlag(DeleteFlagEnum.UN_DELETE_FLAG.getCode());
        ylxtAdmin.setCreateUser("admin");
        ylxtAdmin.setUpdateUser("admin");
        ylxtAdmin.setCreateTime(new Date());
        ylxtAdmin.setUpdateTime(new Date());
        ylxtAdmin.setId(Long.valueOf(id));
        adminService.updateById(ylxtAdmin);
        return ResultBody.success();
    }
}
